/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-20 13:57:03
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-12-30 14:56:46
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { StoreFunctionsServiceProxy, StorePutServiceProxy, StorePutModelContentDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { AddCertificateComponent } from './add-certificate/add-certificate.component';
import { AddReleaseComponent } from './add-release/add-release.component';
import { ManualAddCertificateComponent } from './manual-add-certificate/manual-add-certificate.component';
import { BatchWarehousingComponent } from './batch-warehousing/batch-warehousing.component';
import { UpdateReleaseComponent } from './update-release/update-release.component';
import { UpdateCertificateComponent } from './update-certificate/update-certificate.component';
import { ManualUpdateCertificateComponent } from './manual-update-certificate/manual-update-certificate.component';
import { AddShowResumeComponent } from './add-show-resume/add-show-resume.component';
import { ScanCoedeComponent } from './scan-coede/scan-coede.component';
@Component({
    selector: 'app-warehousing',
    templateUrl: './warehousing.component.html',
    styleUrls: ['./warehousing.component.less']
})
export class WarehousingComponent extends AppComponentBase {
    @ViewChild('AddCertificateComponent') AddCertificateComponent: AddCertificateComponent;
    @ViewChild('ScanCoedeComponent') ScanCoedeComponent: ScanCoedeComponent;
    @ViewChild('AddReleaseComponent') AddReleaseComponent: AddReleaseComponent;
    @ViewChild('ManualAddCertificateComponent') ManualAddCertificateComponent: ManualAddCertificateComponent;
    @ViewChild('BatchWarehousingComponent') BatchWarehousingComponent: BatchWarehousingComponent;
    @ViewChild('UpdateReleaseComponent') UpdateReleaseComponent: UpdateReleaseComponent;
    @ViewChild('UpdateCertificateComponent') UpdateCertificateComponent: UpdateCertificateComponent;
    @ViewChild('ManualUpdateCertificateComponent') ManualUpdateCertificateComponent: ManualUpdateCertificateComponent;
    @ViewChild('AddShowResumeComponent') AddShowResumeComponent: AddShowResumeComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    stroeName = undefined;//入库条件
    stroeNameOptions = [];
    storePutNo = undefined;
    storePutNoOptions = [];
    sstroeName = undefined;//入库查询条件
    productDrawingNo = undefined;
    productDrawingNoOptions = [];
    qualifiedNumber = undefined;
    qualifiedNumberOptions = [];
    taskNumber = undefined;
    taskNumberOptions = [];
    storePutPeople = undefined;
    storePutPeopleOptions = [];
    rowdata: any;
    title = '';
    deleteRow: any = [];
    warehouseData = { label: undefined, value: undefined };
    confirmFlag = false;
    actived = false;
    form = new StorePutModelContentDto();
    binLocationOptions = [];
    typeStatus: string = ""
    cantop: boolean = true;
    display = false;
    saveFlag = true;
    uploadedFiles = [];
    status = undefined;
    confirmTitle = undefined;
    statusOptions = [
        {
            lable: '待入库',
            value: 0
        }, {
            label: '已入库',
            value: 1
        }
    ]
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _storeFunctionsServiceProxy: StoreFunctionsServiceProxy,
        private _storePutServiceProxy: StorePutServiceProxy,
    ) {
        super(injector);
    }
    confirmBatch() {
        if (this.deleteRow.length == 0) {
            this._messageService.info('请先选择一条数据');
            return;
        }
        let storeName = this.deleteRow[0].storeId;
        let binLocationId = this.deleteRow[0].binLocationId;
        let storeFlag = this.deleteRow.find(item => item.storeId != storeName);
        let binLocationFlag = this.deleteRow.find(item => item.binLocationId != binLocationId);
        if (storeFlag == undefined) {
            this.form.storeId = storeName;
        }
        if (binLocationFlag == undefined) {
            this.form.binLocationId = binLocationId;
        }
        this.confirmTitle = '批量入库确认';
        this.confirmFlag = true;
        this.actived = true;
    }
    openUrl() {
        window.open('http://10.20.253.4:8057/#/app/material/into');
    }
    scan() {
        this.title = '扫码入库';
        setTimeout(() => {
            this.ScanCoedeComponent.show();
        }, 10);
    }
    export() {
        this.display = true;
    }
    /**
    * 下载模板
    */
    down() {
        let url = "http://localhost:4200/assets/common/excel/certificate.xls";
        window.open(url);
        URL.revokeObjectURL(url);
    }
    myUploader(event) {
        let files = { data: undefined, fileName: event.files[0].name };
        files.data = event.files[0];
        if (this.saveFlag) {
            this.saveFlag = false;
            this._storePutServiceProxy.getExcelToStorePut(
                files
            ).pipe(finalize(() => this.primengSonTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.saveFlag = true;
                if (result.code == 0) {
                    this._messageService.success("导入成功！");
                    this.uploadedFiles = [];
                    this.display = false;
                    this.getTableList();
                } else {
                    this._messageService.error(result.message);
                    this.uploadedFiles = [];
                    this.display = false;
                }
            });
        }

    }
    deleteWarehose() {
        if (this.deleteRow == undefined || Object.keys(this.deleteRow).length == 0) {
            this._messageService.error('未选择数据！');
            return;
        }
        if (this.cantop) {
            this.cantop = false
            this._storePutServiceProxy.deleteStorePutContent(
                this.deleteRow
            ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this._messageService.success('已取消入库！');
                this.reset();
                this.getTableList();
                this.cantop = true
            });
        }
    }
    toConfirm() {
        for (const item of this.binLocationOptions) {
            if (item.value == this.form.binLocationId) {
                this.form.binLocation = item.label;
            }
        };
        for (const item of this.stroeNameOptions) {
            if (item.value == this.form.storeId) {
                this.form.stroeName = item.label;
            }
        }
        if (this.confirmTitle == '入库确认') {
            if (this.cantop) {
                this.cantop = false
                this._storePutServiceProxy.notarizePutStore(this.form)
                    .pipe(finalize(() => { }))
                    .subscribe(() => {
                        this._messageService.success('保存成功');
                        this.onHide(1);
                        this.getTableList();
                        this.cantop = true;
                    });
            }
        } else {
            if (this.cantop) {
                this.cantop = false
                this._storePutServiceProxy.notarizePutStoreBatch(
                    this.deleteRow,
                    this.form.stroeName,
                    this.form.storeId,
                    this.form.binLocation,
                    this.form.binLocationId
                )
                    .pipe(finalize(() => { }))
                    .subscribe(() => {
                        this._messageService.success('保存成功');
                        this.onHide(1);
                        this.getTableList();
                        this.cantop = true;
                        this.deleteRow = [];
                    });
            }
        }

    }
    onHide(event) {
        this.form = new StorePutModelContentDto();
        this.confirmFlag = false;
        this.actived = false;
    }
    confirm(row) {
        this.confirmTitle = '入库确认';
        this.confirmFlag = true;
        this.form = row;
        this.actived = true;
    }
    lookResume(row) {
        this.rowdata = row;
        setTimeout(() => {
            this.AddShowResumeComponent.show();
            this.AddShowResumeComponent.getTableList();
        }, 10);
    }
    onChange(event) {
        for (const item of this.stroeNameOptions) {
            if (event.value == item.value) {
                this.warehouseData.value = event.value;
                this.warehouseData.label = item.label;
            }
        }
    }
    update(row, type) {
        this.rowdata = row;

        this.typeStatus = type
        if (row.warehousMethod == 0) {
            this.title = `合格证信息${type}`;
            setTimeout(() => {
                this.UpdateCertificateComponent.show();
            }, 10);
        }
        if (row.warehousMethod == 1) {
            this.title = `放行单信息${type}`;
            setTimeout(() => {
                this.UpdateReleaseComponent.show();
            }, 10);
        }
        if (row.warehousMethod == 2) {
            this.title = `合格证手工入库信息${type}`;
            setTimeout(() => {
                this.ManualUpdateCertificateComponent.show();
            }, 10);
        }
    }
    addCertificate() {
        this.title = '新增合格证入库';
        setTimeout(() => {
            this.AddCertificateComponent.show();
        }, 10);
    }
    addRelease() {
        this.title = '新增放行单入库';
        setTimeout(() => {
            this.AddReleaseComponent.show();
        }, 10);
    }
    manualAdd() {
        this.title = '新增合格证手工入库';
        setTimeout(() => {
            this.ManualAddCertificateComponent.show();
        }, 10);
    }
    batchAdd() {
        this.title = "合格证批量入库";
        setTimeout(() => {
            this.BatchWarehousingComponent.show();
        }, 10);
    }
    ngOnInit() {
        //仓库名称
        this._storeFunctionsServiceProxy.getStoreDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.stroeNameOptions = [];
                this.stroeNameOptions = result;
            });
        //入库单号
        this._storePutServiceProxy.getStorePutNoList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.storePutNoOptions = [];
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.storePutNoOptions.push(data);
                }
            });
        //产品图号
        this._storePutServiceProxy.getProductDrawingNoList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.productDrawingNoOptions = [];
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.productDrawingNoOptions.push(data);
                }
            });
        //合格证号
        this._storePutServiceProxy.getQualifiedNumberList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.qualifiedNumberOptions = [];
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.qualifiedNumberOptions.push(data);
                }
            });
        //任务编号
        this._storePutServiceProxy.getQualifiedTaskNumberList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.taskNumberOptions = [];
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.taskNumberOptions.push(data);
                }
            });
        //入库人
        this._storePutServiceProxy.getStorePutPeopleList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.storePutPeopleOptions = [];
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item;
                    data.value = item;
                    this.storePutPeopleOptions.push(data);
                }
            });
        //储位
        this._storeFunctionsServiceProxy.getBinLocationDropList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.binLocationOptions = [];
                this.binLocationOptions = result;
            });
    }
    enum(value) {
        switch (value) {
            case 0:

                return '正常品';
            case 1:

                return '隔离品';
            case 2:

                return '返修品';
            case 3:

                return '报废品';
            case 4:

                return '返修正常品';
            default:
                return '';
        }
    }
    reset() {
        this.status = undefined;
        this.stroeName = undefined;
        this.sstroeName = undefined;
        this.storePutNo = undefined;
        this.productDrawingNo = undefined;
        this.qualifiedNumber = undefined;
        this.taskNumber = undefined;
        this.storePutPeople = undefined;
        this.warehouseData.label = undefined;
        this.warehouseData.value = undefined;
        this.deleteRow = [];
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._storePutServiceProxy.getParentPutStoreGrid(
            this.storePutNo,
            this.sstroeName,
            this.productDrawingNo,
            this.qualifiedNumber,
            this.taskNumber,
            this.storePutPeople,
            this.status,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            //this.primengTableHelper.records = [{ code: 1 },{ code: 2 },{ code: 3 }];
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
}
